// node TestDateAndTime.js
// 示例1：控制台输出"Hello, world!"
// console.log("Hello, world!");
// 示例2：控制台输出标准UTM时区的日期和时间
// const now = new Date();
// console.log(now);
// 示例3：控制台输出当地时区的日期和时间
// const now = new Date();
// console.log(now.toLocaleString("zh-CN"));
// 示例4：计算入睡时间，控制台输出日期和时间
// const fallAsleepTime = new Date();
// fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
// console.log(fallAsleepTime.toLocaleString("zh-CN"));
// 示例5：计算入睡时间，控制台仅输出时间
// const fallAsleepTime = new Date();
// fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
// console.log(fallAsleepTime.toLocaleTimeString("zh-CN", {timeStyle:"short",}));
// 示例6：计算睡眠周期结束时间
// const fallAsleepTime = new Date();
// fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
// console.log("You will fall sleep at", fallAsleepTime.toLocaleTimeString("zh-CN", {timeStyle:"short",}));

// const wakeUpTime = new Date(fallAsleepTime);
// wakeUpTime.setMinutes(wakeUpTime.getMinutes() + 90);
// const wakeUpTimeString = wakeUpTime.toLocaleTimeString("zh-CN", {timeStyle:"short",});
// console.log(`Your first sleep cycle ends at ${wakeUpTimeString}`);
// 示例7：从入睡时间起，计算6个睡眠周期的结束时间
// const fallAsleepTime = new Date();
// fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
// console.log("You will fall sleep at", fallAsleepTime.toLocaleTimeString("zh-CN", {timeStyle:"short",}));
// const wakeUpTime = new Date(fallAsleepTime);
// for (let i = 1; i <= 6; i++){
//     wakeUpTime.setMinutes(wakeUpTime.getMinutes() + 90);
//     const wakeUpTimeString = wakeUpTime.toLocaleTimeString("zh-CN", {timeStyle:"short",});
//     console.log(`Sleep cycle #${i} ends at ${wakeUpTimeString}`);
// }
// 示例8：重构代码：使用数组来存储计算好的醒来时间
// const fallAsleepTime = new Date();
// fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
// console.log("You will fall sleep at", fallAsleepTime.toLocaleTimeString("zh-CN", {timeStyle:"short",}));
// const wakeUpTime = new Date(fallAsleepTime);
// const wakeUpTimes = [];
// for (let i = 1; i <= 6; i++){
//     wakeUpTime.setMinutes(wakeUpTime.getMinutes() + 90);
//     const wakeUpTimeString = wakeUpTime.toLocaleTimeString("zh-CN", {timeStyle:"short",});
//     wakeUpTimes.push(wakeUpTimeString);
// }
// console.log("Wake-up times:", wakeUpTimes.join(", "));

//示例9：将示例8的代码封装到calcWakeUpTimes函数里
// function calcWakeUpTimes(){
//     const fallAsleepTime = new Date();
//     fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
//     console.log("You will fall sleep at", fallAsleepTime.toLocaleTimeString("zh-CN", {timeStyle:"short",}));
//     const wakeUpTime = new Date(fallAsleepTime);
//     const wakeUpTimes = [];
//     for (let i = 1; i <= 6; i++){
//         wakeUpTime.setMinutes(wakeUpTime.getMinutes() + 90);
//         const wakeUpTimeString = wakeUpTime.toLocaleTimeString("zh-CN", {timeStyle:"short",});
//         wakeUpTimes.push(wakeUpTimeString);
//     }
//     console.log("Wake-up times:", wakeUpTimes.join(", "));
// }

// calcWakeUpTimes();

// 示例10：当用户点击按钮时才会调用calcWakeUpTimes
// function calcWakeUpTimes(){
//     const fallAsleepTime = new Date();
//     fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
//     console.log("You will fall sleep at", fallAsleepTime.toLocaleTimeString("zh-CN", {timeStyle:"short",}));
//     const wakeUpTime = new Date(fallAsleepTime);
//     const wakeUpTimes = [];
//     for (let i = 1; i <= 6; i++){
//         wakeUpTime.setMinutes(wakeUpTime.getMinutes() + 90);
//         const wakeUpTimeString = wakeUpTime.toLocaleTimeString("zh-CN", {timeStyle:"short",});
//         wakeUpTimes.push(wakeUpTimeString);
//     }
//     console.log("Wake-up times:", wakeUpTimes.join(", "));
// }

// const calcBtn = document.getElementById("calc-btn");
// calcBtn.onclick = calcWakeUpTimes;

// 示例11：计算睡眠周期，并动态更新网页
// function calcWakeUpTimes(){
//     const fallAsleepTime = new Date();
//     fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
//     const wakeUpTime = new Date(fallAsleepTime);
//     for (let i = 1; i <= 6; i++){
//         wakeUpTime.setMinutes(wakeUpTime.getMinutes() + 90);
//         const wakeUpTimeString = wakeUpTime.toLocaleTimeString("zh-CN", {timeStyle:"short",});
//         const cycleDiv = document.getElementById(`cycle-${i}`);
//         cycleDiv.textContent = wakeUpTimeString;
//     }
// }

// const calcBtn = document.getElementById("calc-btn");
// calcBtn.onclick = calcWakeUpTimes;

// 示例12：计算睡眠周期，并动态创建div元素并更新网页
const wakeUpHoursDiv = document.getElementById("wakeup-hours-div");
function calcWakeUpTimes(){
    const fallAsleepTime = new Date();
    fallAsleepTime.setMinutes(fallAsleepTime.getMinutes() + 14);
    const wakeUpTime = new Date(fallAsleepTime);
    wakeUpHoursDiv.innerHTML = "";
    for (let i = 1; i <= 6; i++){
        wakeUpTime.setMinutes(wakeUpTime.getMinutes() + 90);
        const wakeUpTimeString = wakeUpTime.toLocaleTimeString("zh-CN", {timeStyle:"short",});
        const cycleDiv = document.createElement("div");
        cycleDiv.classList.add("cycle");
        cycleDiv.setAttribute("id", `cycle-${i}`);
        cycleDiv.textContent = wakeUpTimeString;
        wakeUpHoursDiv.appendChild(cycleDiv);
    }
}

const calcBtn = document.getElementById("calc-btn");
calcBtn.onclick = calcWakeUpTimes;
